//package edu.hunau.service;
//
//import edu.hunau.pojo.ClassRoom;
//import edu.hunau.pojo.Course;
//import edu.hunau.pojo.Student;
//
//import java.util.*;
//
///**
// * 成绩管理
// */
//public class GradeManager {
//    /**
//     * 课堂列表
//     * Map中使用课堂的id作为key
//     */
//    private Map<String,ClassRoom> classRooms;
//
//    /**
//     * 学生信息
//     */
//    private Map<String,Student> students;
//
//
//    public GradeManager(){
//
//    }
//
//
//    /**
//     * TODO: 添加学生信息
//     * @param id
//     * @param name
//     * @param major
//     */
//
//    public void addStudent(String id,String name,String major){
//        Student student = new Student(id, name, major);
//        if (students == null) {
//            students = new HashMap<>();
//        }
//        students.put(id, student);
//    }
//
//    /**
//     *TODO: 添加课程信息
//     *
//     * @param id 课堂编号
//     * @param classRoomName 课堂名称
//     * @param course  课程
//     */
//
//    public void addClassRoom(String id, String classRoomName, Course course){
//        if (classRooms == null) {
//            classRooms = new HashMap<>();
//        }
//        classRooms.put(id, new ClassRoom(id, classRoomName, course));
//
//    }
//
//
//    /**
//     * TODO： 为课堂 添加学生
//     * @param classRoomId 课堂的编号
//     * @param studentId 学生学号（可变长参数）
//     */
//    public void addStudent(String classRoomId, String... studentId){
//        ClassRoom classRoom = classRooms.get(classRoomId);
//        if (classRoom == null) {
//            System.out.println("课堂编号不存在！");
//            return;
//        }
//        for (String id : studentId) {
//            Student student = students.get(id);
//            if (student == null) {
//                System.out.println("学生编号不存在：" + id);
//                continue;
//            }
//            classRoom.getTranscript().put(student, null); // 初始成绩为null
//            classRoom.setStudentNum(classRoom.getStudentNum() + 1);
//        }
//
//    }
//
//    /**
//     * TODO: 批量为课堂添加成绩
//     * @param classRoomId
//     * @param grades key为学生学号，value为成绩。
//     */
//    public void addGradeBatch(String classRoomId,Map<String,Integer> grades){
//        ClassRoom classRoom = classRooms.get(classRoomId);
//        if (classRoom == null) {
//            System.out.println("课堂编号不存在！");
//            return;
//        }
//        for (Map.Entry<String, Integer> entry : grades.entrySet()) {
//            String studentId = entry.getKey();
//            Integer grade = entry.getValue();
//            Student student = students.get(studentId);
//            if (student == null) {
//                System.out.println("学生编号不存在：" + studentId);
//                continue;
//            }
//            classRoom.getTranscript().put(student, grade);
//        }
//
//    }
//
//    /**
//     * @param classRoomId
//     */
//    public void printClassRoomMessage(String classRoomId){
//
//        printClassRoomGrade(classRoomId);
//
//        System.out.println("课程平均分:"+classRoomAvgGrade(classRoomId));
//        //打印课堂成绩的正态分布状态
//        System.out.println("成绩正态分布情况:"+isnormalDistribution(classRoomId));
//    }
//
//    /**
//     * todo : 打印课程的成绩 ( 可提供排序功能 )
//     *
//     * @param classRoomId
//     */
//
//    public void printClassRoomGrade(String classRoomId) {
//        ClassRoom classRoom = classRooms.get(classRoomId);
//        if (classRoom == null) {
//            System.out.println("课堂编号不存在！");
//            return;
//        }
//        System.out.println("课堂名称：" + classRoom.getName());
//        System.out.println("课程：" + classRoom.getCourse().toString());
//        System.out.println("学生成绩：");
//        classRoom.getTranscript().entrySet().stream()
//                .sorted(Map.Entry.comparingByKey(Comparator.comparing(Student::getName)))
//                .forEach(entry -> System.out.println(entry.getKey() + ": " + entry.getValue()));
//    }
//
//
//
//    /***
//     * TODO: 自己拓展一个方法：例如：获得某个学生的所有成绩，对学生的成绩进行排序
//     */
//    public void printStudentGrades(String studentId) {
//        List<Map.Entry<ClassRoom, Integer>> studentGrades = new ArrayList<>();
//        for (Map.Entry<String, ClassRoom> entry : classRooms.entrySet()) {
//            ClassRoom classRoom = entry.getValue();
//            Integer grade = classRoom.getTranscript().get(students.get(studentId));
//            if (grade != null) {
//                studentGrades.add(new AbstractMap.SimpleEntry<>(classRoom, grade));
//            }
//        }
//        studentGrades.sort(Map.Entry.comparingByValue());
//        System.out.println("学生 " + studentId + " 的成绩：");
//        studentGrades.forEach(entry -> System.out.println(entry.getKey().getName() + " - " + entry.getValue()));
//    }
//
//
//
//
//    /**
//     * TODO:  获得课程的平均成绩
//     * @param classRoomId
//     * @return
//     */
//    private double classRoomAvgGrade(String classRoomId){
//        ClassRoom classRoom = classRooms.get(classRoomId);
//        if (classRoom == null) {
//            System.out.println("课堂编号不存在！");
//            return 0;
//        }
//        return classRoom.getTranscript().values().stream()
//                .filter(Objects::nonNull)
//                .mapToInt(Integer::intValue)
//                .average()
//                .orElse(0.0);
//
//
//    }
//
//    /**
//     * TODO: 扩展练习，判断课程的成绩是否符合正态分布。（推荐借助大模型完成）
//     * @param classRoomId
//     * @return
//     */
//
//    private String isnormalDistribution(String classRoomId){
//        ClassRoom classRoom = classRooms.get(classRoomId);
//        if (classRoom == null) {
//            System.out.println("课堂编号不存在！");
//            return null;
//        }
//        List<Integer> grades = new ArrayList<>(classRoom.getTranscript().values());
//        grades.removeIf(Objects::isNull); // 去除null值
//        if (grades.size() < 3) {
//            return "样本数量不足，无法判断正态分布";
//        }
//        // 这里仅做简单判断，实际正态分布判断需要更复杂的统计分析
//        double mean = grades.stream().mapToInt(Integer::intValue).average().orElse(0.0);
//        double stdDev = Math.sqrt(grades.stream().mapToDouble(score -> Math.pow(score - mean, 2)).sum() / grades.size());
//        int count = 0;
//        for (Integer grade : grades) {
//            if (Math.abs(grade - mean) <= stdDev) {
//                count++;
//            }
//        }
//        double proportion = count / (double) grades.size();
//
//        return "todo...";
//    }
//
//}

package edu.hunau.service;

import edu.hunau.pojo.ClassRoom;
import edu.hunau.pojo.Course;
import edu.hunau.pojo.Student;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 成绩管理
 */
public class GradeManager {
    private Map<String, ClassRoom> classRooms = new HashMap<>();
    private Map<String, Student> students = new HashMap<>();

    public GradeManager() {
    }

    public void addStudent(String id, String name, String major) {
        students.put(id, new Student(id, name, major));
    }

    public void addClassRoom(String id, String classRoomName, Course course) {
        classRooms.put(id, new ClassRoom(id, classRoomName, course));
    }

    public void addStudent(String classRoomId, String... studentId) {
        ClassRoom classRoom = classRooms.get(classRoomId);
        if (classRoom != null) {
            for (String id : studentId) {
                Student student = students.get(id);
                if (student != null) {
                    classRoom.getTranscript().put(student, null);
                    classRoom.setStudentNum(classRoom.getStudentNum() + 1);
                }
            }
        }
    }

    public void addGradeBatch(String classRoomId, Map<String, Integer> grades) {
        ClassRoom classRoom = classRooms.get(classRoomId);
        if (classRoom != null) {
            grades.forEach((id, score) -> {
                Student student = students.get(id);
                if (student != null) {
                    classRoom.getTranscript().put(student, score);
                }
            });
        }
    }

    public void printClassRoomGrade(String classRoomId) {
        ClassRoom classRoom = classRooms.get(classRoomId);
        if (classRoom != null) {
            System.out.println("课堂名称：" + classRoom.getName());
            System.out.println("课程：" + classRoom.getCourse().getName() + " 课程类型：" + classRoom.getCourse().getType());
            System.out.println("学生成绩：");
            List<Map.Entry<Student, Integer>> sortedEntries = new ArrayList<>(classRoom.getTranscript().entrySet());
            sortedEntries.sort((e1, e2) -> e1.getKey().getName().compareTo(e2.getKey().getName()));
            sortedEntries.forEach(entry -> System.out.println(entry.getKey().getName() + " " + entry.getValue()));
        }
    }

    public void printClassRoomMessage(String classRoomId) {
        printClassRoomGrade(classRoomId);
        System.out.println("课程平均分:" + classRoomAvgGrade(classRoomId));
        System.out.println("成绩正态分布情况:" + isnormalDistribution(classRoomId));
    }

    public void printStudentGrades(String studentId) {
        List<Map.Entry<ClassRoom, Integer>> studentGrades = new ArrayList<>();
        for (Map.Entry<String, ClassRoom> entry : classRooms.entrySet()) {
            ClassRoom classRoom = entry.getValue();
            Integer grade = classRoom.getTranscript().get(students.get(studentId));
            if (grade != null) {
                studentGrades.add(new AbstractMap.SimpleEntry<>(classRoom, grade));
            }
        }
        studentGrades.sort(Map.Entry.comparingByValue());
        System.out.println("学生 " + studentId + " 的成绩：");
        studentGrades.forEach(entry -> System.out.println(entry.getKey().getName() + " - " + entry.getValue()));
    }

    private double classRoomAvgGrade(String classRoomId) {
        ClassRoom classRoom = classRooms.get(classRoomId);
        if (classRoom != null) {
            return classRoom.getTranscript().values().stream()
                    .filter(Objects::nonNull)
                    .mapToInt(Integer::intValue)
                    .average()
                    .orElse(0.0);
        }
        return 0;
    }

    private String isnormalDistribution(String classRoomId) {
        ClassRoom classRoom = classRooms.get(classRoomId);
        if (classRoom != null) {
            List<Integer> grades = classRoom.getTranscript().values().stream()
                    .filter(Objects::nonNull)
                    .collect(Collectors.toList());
            if (grades.size() < 3) {
                return "样本数量不足，无法判断正态分布";
            }
            double mean = grades.stream().mapToInt(Integer::intValue).average().orElse(0);
            double stdDev = Math.sqrt(grades.stream().mapToDouble(g -> Math.pow(g - mean, 2)).sum() / grades.size());
            int count = (int) grades.stream().filter(g -> Math.abs(g - mean) <= stdDev).count();
            double proportion = count / (double) grades.size();
            return proportion >= 0.68 ? "符合正态分布" : "不符合正态分布";
        }
        return "课堂编号不存在";
    }
}
